package com.dxt.netty.utils;

public class ReqStrUtils {

    /**
     * 根据设备上报数据中的校验数据计算CRC16ASCII校验码
     * @param reqStr
     * @return
     */
    public static String signStr(String reqStr){
        //参与校验的数据
        String orgSignStr = reqStr.substring(0,reqStr.length()-5);
        //计算CRC16校验
        int sign = CRC16Util.calcCrc16(orgSignStr.getBytes());
        //计算CRC16校验ASCII
        String signAscII = CRC16Util.getCrc(sign);
        return signAscII;
    }

    /**
     * 获取设备上报数据中的校验码
     * @param reqStr
     * @return
     */
    public static String getSgin(String reqStr){
        String[] dataArrays = reqStr.split(",");
        String sign = (dataArrays[dataArrays.length-1]).substring(0,(dataArrays[dataArrays.length-1]).length()-1);
        return sign;
    }

    /**
     * 封装16进制数据不够8位前面用0补齐
     * @param value
     * @return
     */
    public static String transHex(String value){
        int pLength = 8;//业务id规定长度为8
        int length = value.length();
        StringBuffer prx= new StringBuffer();
        for(int i=0;i<pLength-length;i++){
            prx.append("0");
        }
        return (prx.toString()+value).toUpperCase();
    }

}
